const ffi = require("ffi-napi");
const ref = require("ref-napi");
const path = require("path");
// main();

exports.usePPCSApi = function usePPCSApi() {
  // eslint-disable-next-line no-undef
  const ppcsDir = path.join(__static, "./sdk/lib/PPCS_API.dll");
  console.log("ppcsDir", ppcsDir);
  const libcygHCTP = ffi.Library(
    // eslint-disable-next-line no-undef
    path.join(__static, "./sdk/lib/cygHCTP.dll")
  );
  const libPPCS_API = ffi.Library(ppcsDir);
  console.log("libPPCS_API", libPPCS_API);
  console.log("libcygHCTP", libcygHCTP);
};

exports.useWin32api = function useWin32api() {
  const user32 = new ffi.Library("user32", {
    EnumWindows: ["bool", ["pointer", "int32"]],
    GetWindowTextW: ["int", ["int", "pointer", "int"]],
    SetForegroundWindow: ["bool", ["int"]],
    ShowWindow: ["bool", ["int", "int"]],
    SetWindowPos: ["bool", ["int", "int", "int", "int", "int", "int", "uint"]],
    GetWindowRect: ["bool", ["int32", "pointer"]],
  });

  const windows = [];

  const callback = ffi.Callback(
    "bool",
    ["int", "int"],
    // eslint-disable-next-line no-unused-vars
    function (hwnd, lparam) {
      const buf = Buffer.alloc(1024);
      user32.GetWindowTextW(hwnd, buf, 1024);
      const windowName = ref.reinterpretUntilZeros(buf, 2).toString("ucs2");
      windows.push({ hwnd: hwnd, name: windowName });
      return true;
    }
  );

  user32.EnumWindows(callback, 0);
  console.log("user32", user32);

  console.log("windows", windows);

  // const targetWindowName = /酷狗/gi;
  // const targetWindow = windows.find(function (w) {
  //   if (targetWindowName.test(w.name) && !/桌面歌词/gi.test(w.name)) {
  //     return true;
  //   }
  //   // return targetWindowName.test(w.name);
  //   return false;
  // });

  // const rectPointer = Buffer.alloc(4 * 4);
  // const playButtonPositionFromBottom = 30;

  // if (targetWindow) {
  //   user32.GetWindowRect(targetWindow.hwnd, rectPointer);
  //   user32.ShowWindow(targetWindow.hwnd, 9);
  //   user32.SetWindowPos(targetWindow.hwnd, -1, 0, 0, 0, 0, 0x0002 | 0x0001);
  //   user32.SetForegroundWindow(targetWindow.hwnd);

  //   const targetWindowRect = {};
  //   targetWindowRect.left = rectPointer.readInt16LE(0);
  //   targetWindowRect.top = rectPointer.readInt16LE(4);
  //   targetWindowRect.right = rectPointer.readInt16LE(8);
  //   targetWindowRect.bottom = rectPointer.readInt16LE(12);

  //   const windowWidth = targetWindowRect.right - targetWindowRect.left;
  //   const windowHeight = targetWindowRect.bottom - targetWindowRect.top;
  //   const x = targetWindowRect.left + windowWidth / 2;
  //   const y = targetWindowRect.bottom - playButtonPositionFromBottom;

  //   // console.log("targetWindowRect", targetWindowRect);
  //   // console.log("x, y", x, y);

  //   // 获取第二个参数
  //   // node .\index.js 下一首
  //   const direction = String(process.argv[2]).trim();

  //   if (direction === "上一首") {
  //     // 上一首
  //     robot.moveMouse(x - 45, y);
  //   } else if (direction === "下一首") {
  //     // 下一首
  //     robot.moveMouse(x + 45, y);
  //   } else {
  //     // 播放或者暂停
  //     robot.moveMouse(x, y);
  //   }

  //   robot.mouseClick();

  //   console.log(
  //     "Window '" + targetWindowName + "' is now visible and in focus."
  //   );
  // } else {
  //   console.log("Window '" + targetWindowName + "' not found.");
  // }
};
